package com.example.njut;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class DrawView extends View {
    //设置两个球的坐标
    int i = 40;
    int j = 0;
    float B_x = 40;
    float B_y = 100;
    float R_x = (float) (Math.random()*460+140);
    float R_y = (float) (Math.random()*750+100);
    //设置两支笔
    Paint B = new Paint();
    Paint R = new Paint();
    public DrawView(Context context){
        super(context);
    }

    //创建画布
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);

        //设置画笔颜色
        B.setColor(Color.BLUE);
        R.setColor(Color.RED);
        //设置绘制的图形
        canvas.drawCircle(B_x,B_y,40,B);
        canvas.drawCircle(R_x,R_y,i,R);
    }

    //处理触碰事件
    public boolean onTouchEvent(MotionEvent event){
        //修改蓝球的位置为触碰的位置
        if(event.getX()<(B_x+(50+j*10)) && event.getX()>(B_x-(50+j*10)) && event.getY()<(B_y+(50+j*10)) && event.getY()>(B_y-(50+j*10))){
            B_x = event.getX();
            B_y = event.getY();
        }

        BallActivity.ball.xy.setText("当前蓝球的坐标是："+(int)B_x+"，"+(int)B_y);
        //重新绘图
        invalidate();
        if(B_x<(R_x+40) && B_x>(R_x-40) && B_y<(R_y+40) && B_y>(R_y-40)){
            BallActivity.ball.show(BallActivity.ball);
            i+=10;
            B_x = 40;
            B_y = 100;
            j++;
        }
        return true;
    }
}
